package com.ohos.apollo_hos.adapter;


import com.ohos.apollo_hos.ResourceTable;
import com.ohos.apollo_hos.bean.MainBean;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/**
 * 测试 list adapter
 */
public class NewsListAdapter extends BaseItemProvider {
    private List<MainBean> newsInfoList;
    private Context context;
    public static final int[] imgs={ResourceTable.Media_e1,ResourceTable.Media_e2,ResourceTable.Media_e3,ResourceTable.Media_e4,ResourceTable.Media_e5,ResourceTable.Media_e6,ResourceTable.Media_e6,ResourceTable.Media_e7,ResourceTable.Media_e8,ResourceTable.Media_e9,ResourceTable.Media_e10,ResourceTable.Media_e11,ResourceTable.Media_e12,ResourceTable.Media_e13,ResourceTable.Media_e14,ResourceTable.Media_e15,ResourceTable.Media_e16,ResourceTable.Media_e17,ResourceTable.Media_e18,ResourceTable.Media_e19,ResourceTable.Media_e20,ResourceTable.Media_e21,ResourceTable.Media_e22,ResourceTable.Media_e23,ResourceTable.Media_e24,ResourceTable.Media_e25,ResourceTable.Media_e26,ResourceTable.Media_e27,ResourceTable.Media_e28,ResourceTable.Media_e29,ResourceTable.Media_e30,ResourceTable.Media_e31,ResourceTable.Media_e32,ResourceTable.Media_e33,ResourceTable.Media_e34,ResourceTable.Media_e35,ResourceTable.Media_e36,ResourceTable.Media_e37,ResourceTable.Media_e38,ResourceTable.Media_e39,ResourceTable.Media_e40,ResourceTable.Media_e41,ResourceTable.Media_e42,ResourceTable.Media_e43,ResourceTable.Media_e44,ResourceTable.Media_e45,ResourceTable.Media_e46,ResourceTable.Media_e47,ResourceTable.Media_e48,ResourceTable.Media_e49,ResourceTable.Media_e50,ResourceTable.Media_e51,ResourceTable.Media_e52,ResourceTable.Media_e53,ResourceTable.Media_e54,ResourceTable.Media_e55,ResourceTable.Media_e56,ResourceTable.Media_e57,ResourceTable.Media_e58,ResourceTable.Media_e59,ResourceTable.Media_e60,ResourceTable.Media_e61};

    public NewsListAdapter(List<MainBean> listBasicInfo, Context context) {
        this.newsInfoList = listBasicInfo;
        this.context = context;
    }

    @Override
    public int getCount() {
        return newsInfoList == null ? 0 : newsInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component componentP, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        Component component = componentP;
        if (component == null) {
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_main, null, false);
            viewHolder = new ViewHolder();
            viewHolder.image = (Image) component.findComponentById(ResourceTable.Id_img_icon);
            viewHolder.title = (Text) component.findComponentById(ResourceTable.Id_tx_title);
            viewHolder.content = (Text) component.findComponentById(ResourceTable.Id_tx_content);
            component.setTag(viewHolder);
        } else {
            if (component.getTag() instanceof ViewHolder) {
                viewHolder = (ViewHolder) component.getTag();
            }
        }
        if (null != viewHolder) {
            viewHolder.image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
            MainBean bean= (MainBean) getItem(position);
            viewHolder.title.setText(bean.getTitle()+"");
            viewHolder.content.setText(bean.getContent()+"");
            viewHolder.image.setPixelMap(imgs[bean.getImg()]);
        }
        return component;
    }

    /**
     * ViewHolder which has title and image
     */
    private static class ViewHolder {
        private Text title;
        private Text content;
        private Image image;
    }
}
